summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_client_port.cpp
blob: 11b1b977efe46cdf9a32723b64605bebe125bb2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-FileCopyrightText: 2021 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/scope_exit.h"
#include "core/hle/kernel/k_client_port.h"
#include "core/hle/kernel/k_light_session.h"
#include "core/hle/kernel/k_port.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_scoped_resource_reservation.h"
#include "core/hle/kernel/k_session.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {

KClientPort::KClientPort(KernelCore& kernel) : KSynchronizationObject{kernel} {}
KClientPort::~KClientPort() = default;

void KClientPort::Initialize(KPort* parent, s32 max_sessions) {
    // Set member variables.
    m_num_sessions = 0;
    m_peak_sessions = 0;
    m_parent = parent;
    m_max_sessions = max_sessions;
}

void KClientPort::OnSessionFinalized() {
    KScopedSchedulerLock sl{m_kernel};

    if (const auto prev = m_num_sessions--; prev == m_max_sessions) {
        this->NotifyAvailable();
    }
}

void KClientPort::OnServerClosed() {}

bool KClientPort::IsLight() const {
    return this->GetParent()->IsLight();
}

bool KClientPort::IsServerClosed() const {
    return this->GetParent()->IsServerClosed();
}

void KClientPort::Destroy() {
    // Note with our parent that we're closed.
    m_parent->OnClientClosed();

    // Close our reference to our parent.
    m_parent->Close();
}

bool KClientPort::IsSignaled() const {
    return m_num_sessions.load() < m_max_sessions;
}

Result KClientPort::CreateSession(KClientSession** out) {
    // Declare the session we're going to allocate.
    KSession* session{};

    // Reserve a new session from the resource limit.
    //! FIXME: we are reserving this from the wrong resource limit!
    KScopedResourceReservation session_reservation(
        m_kernel.ApplicationProcess()->GetResourceLimit(), LimitableResource::SessionCountMax);
    R_UNLESS(session_reservation.Succeeded(), ResultLimitReached);

    // Allocate a session normally.
    // TODO: Dynamic resource limits
    session = KSession::Create(m_kernel);

    // Check that we successfully created a session.
    R_UNLESS(session != nullptr, ResultOutOfResource);

    // Update the session counts.
    {
        ON_RESULT_FAILURE {
            session->Close();
        };

        // Atomically increment the number of sessions.
        s32 new_sessions{};
        {
            const auto max = m_max_sessions;
            auto cur_sessions = m_num_sessions.load(std::memory_order_acquire);
            do {
                R_UNLESS(cur_sessions < max, ResultOutOfSessions);
                new_sessions = cur_sessions + 1;
            } while (!m_num_sessions.compare_exchange_weak(cur_sessions, new_sessions,
                                                           std::memory_order_relaxed));
        }

        // Atomically update the peak session tracking.
        {
            auto peak = m_peak_sessions.load(std::memory_order_acquire);
            do {
                if (peak >= new_sessions) {
                    break;
                }
            } while (!m_peak_sessions.compare_exchange_weak(peak, new_sessions,
                                                            std::memory_order_relaxed));
        }
    }

    // Initialize the session.
    session->Initialize(this, m_parent->GetName());

    // Commit the session reservation.
    session_reservation.Commit();

    // Register the session.
    KSession::Register(m_kernel, session);
    ON_RESULT_FAILURE {
        session->GetClientSession().Close();
        session->GetServerSession().Close();
    };

    // Enqueue the session with our parent.
    R_TRY(m_parent->EnqueueSession(std::addressof(session->GetServerSession())));

    // We succeeded, so set the output.
    *out = std::addressof(session->GetClientSession());
    R_SUCCEED();
}

Result KClientPort::CreateLightSession(KLightClientSession** out) {
    // Declare the session we're going to allocate.
    KLightSession* session{};

    // Reserve a new session from the resource limit.
    KScopedResourceReservation session_reservation(GetCurrentProcessPointer(m_kernel),
                                                   Svc::LimitableResource::SessionCountMax);
    R_UNLESS(session_reservation.Succeeded(), ResultLimitReached);

    // Allocate a session normally.
    // TODO: Dynamic resource limits
    session = KLightSession::Create(m_kernel);

    // Check that we successfully created a session.
    R_UNLESS(session != nullptr, ResultOutOfResource);

    // Update the session counts.
    {
        ON_RESULT_FAILURE {
            session->Close();
        };

        // Atomically increment the number of sessions.
        s32 new_sessions;
        {
            const auto max = m_max_sessions;
            auto cur_sessions = m_num_sessions.load(std::memory_order_acquire);
            do {
                R_UNLESS(cur_sessions < max, ResultOutOfSessions);
                new_sessions = cur_sessions + 1;
            } while (!m_num_sessions.compare_exchange_weak(cur_sessions, new_sessions,
                                                           std::memory_order_relaxed));
        }

        // Atomically update the peak session tracking.
        {
            auto peak = m_peak_sessions.load(std::memory_order_acquire);
            do {
                if (peak >= new_sessions) {
                    break;
                }
            } while (!m_peak_sessions.compare_exchange_weak(peak, new_sessions,
                                                            std::memory_order_relaxed));
        }
    }

    // Initialize the session.
    session->Initialize(this, m_parent->GetName());

    // Commit the session reservation.
    session_reservation.Commit();

    // Register the session.
    KLightSession::Register(m_kernel, session);
    ON_RESULT_FAILURE {
        session->GetClientSession().Close();
        session->GetServerSession().Close();
    };

    // Enqueue the session with our parent.
    R_TRY(m_parent->EnqueueSession(std::addressof(session->GetServerSession())));

    // We succeeded, so set the output.
    *out = std::addressof(session->GetClientSession());
    R_SUCCEED();
}

} // namespace Kernel